#include "FbxAnimationCurve.h"

namespace GC
{	
	GC::FbxAnimationCurve::FbxAnimationCurve( FbxAnimCurve* a_curve, const double a_deltaFrameTime )		
	{
		//If a_curve is null
		if (a_curve == NULL)
		{			
			m_frameCount = 0;			
		}
		else
		{
			m_frameCount = a_curve->KeyGetCount();			

			for (int i = 0; i < m_frameCount; ++i)
			{
				FbxTime f_time = a_curve->KeyGetTime(i);			
			
				const int f_frameNumber = (int)(f_time.GetSecondDouble() / a_deltaFrameTime);
				const double f_frameValue = static_cast<double>(a_curve->KeyGetValue(i) );

				m_frameValues.insert( std::make_pair( f_frameNumber, f_frameValue ) );				
			}
		}
	}

	/// <summary>
	/// Determines whether this instance is valid by checking if it has at least 1 frame
	/// </summary>
	/// <returns></returns>
	const bool GC::FbxAnimationCurve::IsValid() const
	{
		return m_frameCount > 0;
	}	

	const bool GC::FbxAnimationCurve::HasValueAtFrame( const int a_frameNumber )
	{		
		return m_frameValues.find( a_frameNumber) != m_frameValues.end();
	}

	const double GC::FbxAnimationCurve::GetValueAtFrame( const int a_frameNumber )
	{
		if (HasValueAtFrame( a_frameNumber ) )
		{
			return m_frameValues[a_frameNumber];
		}
		//No way to flag if the 0 is wrong or not ..
		else return 0;
	}

	const int GC::FbxAnimationCurve::GetAnimationLength() const
	{
		if (m_frameCount == 0) return -1;
		//Return value + 1 because frameNumbers are 0 index based so 0 would be 1 frame!
		return m_frameValues.rbegin()->first + 1;
	}

}